//
//  ProvinceExtension.m
//  VietQuan
//
//  Created by hainguyen on 11/1/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ProvinceExtension.h"
#import "Province.h"
#import "AppDelegate_Shared.h"
#import "District.h"
#import "DistrictExtention.h"
#import "CoreDataHelper.h"

@implementation Province(Extension)

#pragma mark Init Data Methods

+ (BOOL) initProvince{
	NSError *err = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil)
	{
		return NO;
	}
	NSMutableArray *provinceArray = [[[NSMutableArray alloc] init]autorelease];
	NSMutableDictionary *provinceDict = [[NSMutableDictionary alloc] init];
	NSMutableArray *districtArray = [[NSMutableArray alloc] init];
	
	[provinceDict setObject:[NSNumber numberWithInt:1] forKey:@"id"];
	[provinceDict setObject:@"tp. Hồ Chí Minh" forKey:@"name"];
	
	[districtArray addObject:@"Quận Nhất"];
	[districtArray addObject:@"Quận Hai"];
	[districtArray addObject:@"Quận Ba"];
	[districtArray addObject:@"Quận Bốn"];
	[districtArray addObject:@"Quận Năm"];
	[districtArray addObject:@"Quận Sáu"];
	[districtArray addObject:@"Quận Bảy"];
	[districtArray addObject:@"Quận Tám"];
	[districtArray addObject:@"Quận Chín"];
	[districtArray addObject:@"Quận Mười"];
	[districtArray addObject:@"Quận Mười Một"];
	[districtArray addObject:@"Quận Mười Hai"];
	[districtArray addObject:@"Bình Thạnh"];
	[districtArray addObject:@"Gò Vấp"];
	[districtArray addObject:@"Phú Nhuận"];
	[districtArray addObject:@"Tân Bình"];
	[districtArray addObject:@"Thủ Đức"];
	[districtArray addObject:@"Bình Tân"];
	[districtArray addObject:@"Bình Chánh"];
	[districtArray addObject:@"Hóc Môn"];
	[districtArray addObject:@"Cần Giờ"];
	[districtArray addObject:@"Củ Chi"];
	[districtArray addObject:@"Nhà Bè"];
	
	[provinceDict setObject:districtArray forKey:@"district"];
	[provinceArray addObject:provinceDict];	
	[provinceDict release];
	[districtArray release];
	
	provinceDict = [[NSMutableDictionary alloc] init];
	districtArray = [[NSMutableArray alloc] init];
	[provinceDict setObject:[NSNumber numberWithInt:2] forKey:@"id"];
	[provinceDict setObject:@"Hà Nội" forKey:@"name"];
	
	[districtArray addObject:@"Ba Đình"];
	[districtArray addObject:@"Hoàng Kiếm"];
	[districtArray addObject:@"Cầu Giấy"];
	[districtArray addObject:@"Hồ Gươm"];
	
	[provinceDict setObject:districtArray forKey:@"district"];
	[provinceArray addObject:provinceDict];
	[provinceDict release];
	[districtArray release];
	
	provinceDict = [[NSMutableDictionary alloc] init];
	[provinceDict setObject:[NSNumber numberWithInt:3] forKey:@"id"];
	[provinceDict setObject:@"Đà Nẵng" forKey:@"name"];
	[provinceArray addObject:provinceDict];
	[provinceDict release];
	
	provinceDict = [[NSMutableDictionary alloc] init];
	[provinceDict setObject:[NSNumber numberWithInt:4] forKey:@"id"];
	[provinceDict setObject:@"Hải Phòng" forKey:@"name"];
	[provinceArray addObject:provinceDict];
	[provinceDict release];
	
	provinceDict = [[NSMutableDictionary alloc] init];
	[provinceDict setObject:[NSNumber numberWithInt:5] forKey:@"id"];
	[provinceDict setObject:@"Cần Thơ" forKey:@"name"];
	[provinceArray addObject:provinceDict];
	[provinceDict release];
	
	provinceDict = [[NSMutableDictionary alloc] init];
	[provinceDict setObject:[NSNumber numberWithInt:6] forKey:@"id"];
	[provinceDict setObject:@"Nha TRang" forKey:@"name"];
	[provinceArray addObject:provinceDict];
	[provinceDict release];
	
	provinceDict = [[NSMutableDictionary alloc] init];
	[provinceDict setObject:[NSNumber numberWithInt:7] forKey:@"id"];
	[provinceDict setObject:@"Vũng Tàu" forKey:@"name"];
	[provinceArray addObject:provinceDict];
	[provinceDict release];
	
	for (int i = 0; i < [provinceArray count]; i++) {
		NSMutableDictionary *dict = [provinceArray objectAtIndex:i];
		Province *province = [NSEntityDescription insertNewObjectForEntityForName:@"Province" inManagedObjectContext:managedObjectContext];
		province.id =  [dict objectForKey:@"id"];
		province.name = [dict objectForKey:@"name"];
		[District initDistrict:[province.id intValue] districtList:[dict objectForKey:@"district"]];
	}
	[managedObjectContext save:&err];
	return YES;
}

#pragma mark Get Methods

+ (NSArray *) getProvinces{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;	
	}
	NSArray *lists;
	@try {
		lists = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Province" :nil :nil :NO :managedObjectContext];
	}@catch (NSException * e) {
		NSLog(@"%@",[e description]);
    }
	
	return lists;
}

+ (Province *) getProvince:(int)provinceId
{
	Province *province = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *provinceArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"id = %d", provinceId];	
	@try {
		provinceArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Province" :predicate :nil :NO :managedObjectContext];
		
		if ([provinceArray count] != 0)
		{
			province = [provinceArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return province;
}

#pragma mark Search Methods

+ (NSArray*)searchByName:(NSString*) keyword{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return nil;	
    }
    NSArray *resultArray;	
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name contains[cd] %@", keyword];
    @try 
    {
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Province" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e) 
    {
        NSLog(@"search by name error: %@", [e description]);
    }
    
    return resultArray;
}

@end
